$('#avatar').editable({
    type: 'image',
    name: 'avatar',
    value: null,
    image: {
        btn_choose: '请选择相片',
        droppable: true,
        maxSize: 110000, //~100Kb
        name: 'avatar',
        on_error: function(error_type) {
            if (last_gritter) $.gritter.remove(last_gritter);
            if (error_type == 1) { //file format error
                last_gritter = $.gritter.add({
                    title: '文件格式错误',
                    text: '支持的上传格式有 jpg | gif | png ',
                    class_name: 'gritter-error gritter-center',
                    time: 2000
                });
            } else if (error_type == 2) { //file size rror
                last_gritter = $.gritter.add({
                    title: '文件太大',
                    text: '文件大少不能大于100kb!!',
                    class_name: 'gritter-error gritter-center',
                    time: 2000
                });
            } else { //other error
            }
        },
        on_success: function() {
            $.gritter.removeAll();
        }
    },
    url: function(params) {
        // ***UPDATE AVATAR HERE*** //
        var submit_url = 'uploadAvatar'; //please modify submit_url accordingly
        var deferred = null;
        var avatar = '#avatar';

        var value = $(avatar).next().find('input[type=hidden]:eq(0)').val();
        if (!value || value.length == 0) {
            deferred = new $.Deferred
            deferred.resolve();
            return deferred.promise();
        }

        var $form = $(avatar).next().find('.editableform:eq(0)')
        var file_input = $form.find('input[type=file]:eq(0)');
        var pk = $(avatar).attr('data-pk'); //primary key to be sent to server

        var ie_timeout = null

        if ("FormData" in window) {
            var formData_object = new FormData(); //create empty FormData object

            $.each($form.serializeArray(), function(i, item) {
                formData_object.append(item.name, item.value);
            });
            $form.find('input[type=file]').each(function() {
                var field_name = $(this).attr('name');
                var files = $(this).data('ace_input_files');
                if (files && files.length > 0) {
                    formData_object.append(field_name, files[0]);
                }
            });

            formData_object.append('pk', pk);

            deferred = $.ajax({
                url: submit_url,
                type: 'POST',
                processData: false, //important
                contentType: false, //important
                dataType: 'json', //server response type
                data: formData_object
            })
        }

        //deferred callbacks, triggered by both ajax and iframe solution
        deferred
            .done(function(res) { //success
                // var res = result[0]; //the `result` is formatted by your server side response and is arbitrary
                if (res.code == '1')
                {
                 $(avatar).get(0).src = res.url;
                 $("#avatarpic").val(res.url);
                }
                else{
                    bootbox.alert(res.msg);
                }
            })
            .fail(function(result) { //failure
                alert("There was an error");
            })
            .always(function() { //called on both success and failure
                if (ie_timeout) clearTimeout(ie_timeout)
                ie_timeout = null;
            });

        return deferred.promise();
    },

    success: function(response, newValue) {}
})